package com.lf.sys.util;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class CommonUtils {
	private static final String dateFormat = "yy-MM-dd HH:mm";

	public static boolean StringToBoolean(String str) {
		if (StringUtils.isBlank(str)) {
			return false;
		}
		if (str.equalsIgnoreCase("true")) {
			return true;
		}
		return false;
	}

	public static String getDateStr(Date date) {
		String temp = "";
		if (date != null) {
			try {
				temp = new SimpleDateFormat(CommonUtils.dateFormat).format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return temp;
	}

	public static String getDateStr(long date) {
		String temp = "";
		if (date > 0) {
			try {
				Date d = new Date(date);
				temp = new SimpleDateFormat(CommonUtils.dateFormat).format(d);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return temp;
	}

	public static String getString(String temp) {
		if (StringUtils.isBlank(temp)) {
			return "";
		}
		return temp.trim();
	}
	public static String getStringNoTrim(String temp) {
		if (StringUtils.isBlank(temp)) {
			return "";
		}
		return temp;
	}

	public static void print(HttpServletRequest request) {
		Enumeration<String> test = request.getParameterNames();
		while (test.hasMoreElements()) {
			String string = (String) test.nextElement();
			System.out.println(string + ": " + request.getParameter(string));
		}
	}
	
	public static void saveFile(byte[] file, String filePath, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
	}
}
